"""错误码定义和映射"""

from enum import Enum
from typing import Dict


class ErrorCode(str, Enum):
    """错误码枚举"""

    # 成功
    SUCCESS = "10001"
    NO_DATA = "10002"

    # 系统错误
    SYSTEM_ERROR = "10003"
    DATABASE_ERROR = "30001"
    SYSTEM_BUSY = "40001"
    INTERFACE_NOT_ALLOWED = "40002"
    HOSPITAL_NOT_OPEN = "40003"
    BUSINESS_PROCESSING = "40004"
    UNKNOWN_ERROR = "90001"

    # 参数错误
    MISSING_PARAMETER = "20001"
    PARAMETER_TYPE_ERROR = "20002"
    PARAMETER_INVALID = "20003"
    PARAMETER_OUT_OF_RANGE = "20004"

    # 权限错误
    NO_ACCESS = "50001"

    # 医院信息错误
    HOSPITAL_NOT_EXIST = "60001"
    DOCTOR_NOT_EXIST = "60002"

    # 支付相关错误
    CARD_NOT_ALLOW_PAYMENT = "60101"
    PAYMENT_INFO_NOT_EXIST = "60102"
    PAYMENT_AMOUNT_MISMATCH = "60103"
    CHARGE_ITEM_NOT_EXIST = "60104"

    # 挂号相关错误
    SPECIAL_FREE_REGISTRATION = "60201"
    REGISTRATION_AMOUNT_MISMATCH = "60202"
    ALREADY_VISITED = "60203"
    REGISTRATION_NOT_EXIST = "60204"
    NO_AVAILABLE_SLOTS = "60205"
    DOCTOR_SUSPENDED = "60206"
    GENDER_RESTRICTION = "60207"
    AGE_RESTRICTION = "60208"
    OVERDUE_CHECKIN = "60209"
    REGISTRATION_LIMIT_EXCEEDED = "60210"
    DEPARTMENT_DAILY_LIMIT = "60211"
    DOCTOR_DAILY_LIMIT = "60212"
    DEPARTMENT_REGISTRATION_LIMIT = "60213"
    DOCTOR_REGISTRATION_LIMIT = "60214"
    CARD_NOT_EXIST = "20215"

    # 充值相关错误
    CARD_NOT_ALLOW_RECHARGE = "60301"
    BUSINESS_NOT_OPENED = "60302"
    RECHARGE_FAILED = "60303"

    # 住院押金相关错误
    NOT_ALLOW_DEPOSIT = "60401"
    PATIENT_DISCHARGED = "60402"

    # 卡相关错误
    ID_CARD_REQUIRED = "60501"
    CARD_CANCELLED = "60502"
    CARD_LOST = "60503"
    CARD_LOCKED = "60504"
    MOBILE_REQUIRED = "60505"
    CARD_INFO_NOT_EXIST = "60506"

    # 退款相关错误
    ITEM_EXECUTED = "60601"

    # 分诊相关错误
    ALREADY_TRIAGED = "60701"


# 错误码映射表
ERROR_CODE_MAPPING: Dict[str, Dict[str, str]] = {
    ErrorCode.SUCCESS: {
        "msg": "成功",
        "description": "操作成功完成"
    },
    ErrorCode.NO_DATA: {
        "msg": "未查询到数据",
        "description": "没有找到符合条件的数据"
    },
    ErrorCode.SYSTEM_ERROR: {
        "msg": "系统错误",
        "description": "系统内部错误"
    },
    ErrorCode.DATABASE_ERROR: {
        "msg": "数据库操作异常",
        "description": "包括更新、查询、插入、失去数据库连接等"
    },
    ErrorCode.SYSTEM_BUSY: {
        "msg": "系统繁忙",
        "description": "压力过大无法应答"
    },
    ErrorCode.INTERFACE_NOT_ALLOWED: {
        "msg": "接口不允许调用",
        "description": "当前接口不允许调用"
    },
    ErrorCode.HOSPITAL_NOT_OPEN: {
        "msg": "医院没有开通本接口",
        "description": "医院未开通此接口服务"
    },
    ErrorCode.BUSINESS_PROCESSING: {
        "msg": "业务处理中",
        "description": "同一笔订单需要保持幂等性"
    },
    ErrorCode.UNKNOWN_ERROR: {
        "msg": "系统未知错误",
        "description": "系统发生未知错误"
    },
    ErrorCode.MISSING_PARAMETER: {
        "msg": "缺少参数",
        "description": "请求参数不完整"
    },
    ErrorCode.PARAMETER_TYPE_ERROR: {
        "msg": "参数类型错误",
        "description": "参数类型不符合要求"
    },
    ErrorCode.PARAMETER_INVALID: {
        "msg": "参数无效或格式错误",
        "description": "参数值无效或格式不正确"
    },
    ErrorCode.PARAMETER_OUT_OF_RANGE: {
        "msg": "参数超出范围",
        "description": "参数值超出允许范围"
    },
    ErrorCode.NO_ACCESS: {
        "msg": "无权访问该接口",
        "description": "鉴权失败或无权限访问"
    },
    ErrorCode.HOSPITAL_NOT_EXIST: {
        "msg": "医院信息不存在",
        "description": "指定的医院信息不存在"
    },
    ErrorCode.DOCTOR_NOT_EXIST: {
        "msg": "医生信息不存在",
        "description": "指定的医生信息不存在"
    },
    ErrorCode.ID_CARD_REQUIRED: {
        "msg": "身份证号不能为空",
        "description": "身份证号是必填参数"
    },
    ErrorCode.CARD_INFO_NOT_EXIST: {
        "msg": "卡号信息不存在",
        "description": "就诊卡号信息不存在"
    }
}


def get_error_message(code: str) -> str:
    """根据错误码获取错误消息"""
    error_info = ERROR_CODE_MAPPING.get(code, {})
    return error_info.get("msg", "未知错误")


def get_error_description(code: str) -> str:
    """根据错误码获取错误描述"""
    error_info = ERROR_CODE_MAPPING.get(code, {})
    return error_info.get("description", "未知错误描述")


def get_all_error_codes() -> Dict[str, Dict[str, str]]:
    """获取所有错误码映射"""
    return ERROR_CODE_MAPPING.copy()